//
//  LaunchViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/27.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "LaunchViewController.h"
#import "LaunchScrollView.h"
#import "constant.h"

@interface LaunchViewController ()

@property (nonatomic, strong) LaunchScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *imageViewAr;
@property (nonatomic, assign) NSNumber *index;
@property (nonatomic, assign) int count;
@property (nonatomic, strong) NSArray *orderStartAr;
@property (nonatomic, strong) NSArray *orderEndAr;
@property (nonatomic, strong) NSTimer *startTimer;
@property (nonatomic, strong) NSTimer *endTimer;

@end

@implementation LaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    bgImageView.image = [UIImage imageNamed:@"Default"];
    [self.view addSubview:bgImageView];//背景图片
    
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *launchPath = [docPath stringByAppendingPathComponent:@"launch.infomation"];
    NSError *error = [[NSError alloc] init];
//    NSLog(@"%@",launchPath);      //沙盒路径

    NSString *Str = [NSString stringWithContentsOfFile:launchPath encoding:NSUTF8StringEncoding error:&error];
    self.count = 0;
    [self addImageAnimation];
    
    if ([Str length] == 0) {//通过查看有没有第一次使用的标语，来判断是否为第一次使用，从而是否要创建引导页
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction) name:@"clickEnterBtn" object:nil];
        _scrollView = [[LaunchScrollView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, JWScreenHeight)];
        [self.view addSubview:_scrollView];
        
        NSString *launchStr = @"欢迎使用JWMovie！";
        [launchStr writeToFile:launchPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
        
        
    }else {
        
        [self playImages];//不是第一次使用，忽略引导页，直接进入启动效果播放界面
    }
    
}

/*
 * 通过通知来实现
 */

- (void)notificationAction{
    
    [_scrollView removeFromSuperview];
    [self playImages];
}

- (void)addImageAnimation{
    
    _imageViewAr = [NSMutableArray array];//多设一个，以为最后一个不调用，（通知的特性）
    int num = 28;
    int col = 4;
    CGFloat spacing = 2;
    CGFloat imageViewWidht = (JWScreenWidth - spacing * 5) / 4.f ;
    CGFloat imageViewHeight = (JWScreenHeight - spacing * 8) / 7.f ;
    
    for (int i = 0; i < num; i++) {
        
        int c = i % col;//列
        int r = i / col;//行
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((imageViewWidht + spacing) * c + spacing, (imageViewHeight + spacing) * r + spacing, imageViewWidht, imageViewHeight)];
        
        NSString *str = [NSString stringWithFormat:@"%d@2x",i + 1];
        imageView.image = [UIImage imageNamed:str];
        
        imageView.hidden = YES;//先隐藏在显示
        [self.view addSubview:imageView];
        
        [_imageViewAr addObject:imageView];
        
    }
    
}

- (void)playImages{
    
    _orderStartAr = @[@0,@1,@2,@3,@7,@11,@15,@19,@23,@27,@26,@25,@24,@20,@16,@12,@8,@4,@5,@6,@10,@14,@18,@22,@21,@17,@13,@9];
    //    将播放显示的顺序加载倒数组中；(多加一个)
    _orderEndAr = @[@0,@24,@25,@20,@16,@21,@26,@27,@22,@17,@12,@8,@13,@18,@23,@19,@14,@9,@4,@0,@5,@10,@15,@11,@6,@1,@2,@7,@3];
    //    将播放隐藏的顺序加载倒数组中；(多加一个，因为最后一个不执行)
    
    _startTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(setimageAppear) userInfo:nil repeats:YES];
    
}

- (void)setimageAppear{
    
    _index = _orderStartAr[_count];
    int i = [_index intValue];
    
    UIView *view = _imageViewAr[i];//取出顺序中规定的imageview
    view.hidden = NO;
    _count++;
    
    if (_count == _orderStartAr.count) {//关闭计时器
        [_startTimer invalidate];
        _endTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(setImagesHiden) userInfo:nil repeats:YES];
    }
    
}

- (void)setImagesHiden{
    _index = _orderEndAr[_count];
    int i = [_index intValue];
    
    UIView *view = _imageViewAr[i];//取出顺序中规定的imageview
    view.hidden = YES;
    _count--;
    
    if (_count < 0) {//关闭计时器
        [_endTimer invalidate];
        
        /*--------------------------播放结束，进入主界面-----------------------------------*/
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeViewController" object:nil userInfo:nil];
        //        播放结束发出通知，让AppDelegate切换控制器
        
    }
   
}





//启动页，不让状态栏显示
- (BOOL)prefersStatusBarHidden{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
